/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.thienloc.omap.controller.services;

import com.thienloc.omap.entity.ServiceConfig;
import com.thienloc.omap.util.CommonUtil;
import com.thienloc.omap.util.Constant;
import com.thienloc.omap.util.JsfUtil;
import java.util.LinkedList;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

/**
 *
 * @author GiangLT
 */
@ManagedBean
@ViewScoped
public class X2ConfigBean extends AbstractServiceConfigBean {

    private List<String> heavenlyStems;
    private List<String> earthlyBranches;
    private List<String> ages;
    private String numbers;

    @Override
    @PostConstruct
    public void init() {
        super.init();
        heavenlyStems = new LinkedList<String>();
        heavenlyStems.add("Giáp");
        heavenlyStems.add("Ất");
        heavenlyStems.add("Bính");
        heavenlyStems.add("Đinh");
        heavenlyStems.add("Mậu");
        heavenlyStems.add("Kỷ");
        heavenlyStems.add("Canh");
        heavenlyStems.add("Tân");
        heavenlyStems.add("Nhâm");
        heavenlyStems.add("Quý");

        earthlyBranches = new LinkedList<String>();
        earthlyBranches.add("Tý");
        earthlyBranches.add("Sửu");
        earthlyBranches.add("Dần");
        earthlyBranches.add("Mão");
        earthlyBranches.add("Thìn");
        earthlyBranches.add("Tỵ");
        earthlyBranches.add("Ngọ");
        earthlyBranches.add("Mùi");
        earthlyBranches.add("Thân");
        earthlyBranches.add("Dậu");
        earthlyBranches.add("Tuất");
        earthlyBranches.add("Hợi");

        ages = new LinkedList<String>();
        for (int i = 0; i < 60; i++) {
            int stemIdx = i % 10;
            int brachIdx = i % 12;
            ages.add(heavenlyStems.get(stemIdx) + " " + earthlyBranches.get(brachIdx));
        }
    }

    public void addAllNumbers() {
        try {
            String[] splitedNumbers = numbers.split("\\n");
            if (splitedNumbers.length != 120) {
                JsfUtil.addErrorMessage("Sai số lượng", "Bạn đã nhập " + splitedNumbers.length + " số, cần nhập đúng 120 số vào dãy");
                return;
            }

            for (int i = 0; i < splitedNumbers.length; i++) {
                if (i % 2 == 0) {
                    String favoriteNumber = splitedNumbers[i].trim() + "-" + splitedNumbers[i + 1].trim();

                    ServiceConfig serviceConfig = new ServiceConfig();
                    serviceConfig.setEndEffect(selected.getEndEffect());
                    serviceConfig.setStartEffect(selected.getStartEffect());
                    serviceConfig.setContent(/*
                             * "Ngay ~ngay~: ban sinh nam ~nam~, ban hop voi so
                             * " +
                             */favoriteNumber);
                    serviceConfig.setCpNumber(selected.getCpNumber());
                    serviceConfig.setDeliverContent("X2 ~nam~");
                    serviceConfig.setParam(CommonUtil.nativeToSimplified(ages.get(i / 2)));
                    serviceConfig.setServiceName(Constant.X2_SERVICE_NAME);
                    facade.create(serviceConfig);
                }
            }
            JsfUtil.addSuccessMessage("Thành công", "Đã thêm thành công 120 bản ghi");
            resetToCreateForm();
        } catch (Exception ex) {
            logAndNotifyError(ex);
        }

    }

    @Override
    protected void validateForm() throws Exception {
    }

    @Override
    protected Logger getLogger() {
        return LoggerFactory.getLogger(X2ConfigBean.class);
    }

    @Override
    protected void resetToCreateFormAction() throws Exception {
        super.resetToCreateFormAction();

        list = facade.findByServiceName(Constant.X2_SERVICE_NAME, startViewDate, endViewDate);
        selected.setServiceName(Constant.X2_SERVICE_NAME);
        selected.setDeliverContent(Constant.X2_SERVICE_NAME);

        edit = false;
        view = false;
    }

    public List<String> getEarthlyBranches() {
        return earthlyBranches;
    }

    public void setEarthlyBranches(List<String> earthlyBranches) {
        this.earthlyBranches = earthlyBranches;
    }

    public List<String> getHeavenlyStems() {
        return heavenlyStems;
    }

    public void setHeavenlyStems(List<String> heavenlyStems) {
        this.heavenlyStems = heavenlyStems;
    }

    public String getNumbers() {
        return numbers;
    }

    public void setNumbers(String numbers) {
        this.numbers = numbers;
    }

    public List<String> getAges() {
        return ages;
    }

    public List<String> getEarthlyBranchMap() {
        return earthlyBranches;
    }

    public List<String> getHeavenlyStemMap() {
        return heavenlyStems;
    }
}
